﻿using System;
using System.Collections.Generic;
using System.Data;
using FoundationClass.Entity;

namespace FoundationClass.Data
{
    public static class UserData
    {
        public static void InsertUser(User user)
        {
            DataTable userDt = DatabaseManager.Read("User");

            userDt.Rows.Add(user.Id, user.Username, user.Password, user.FirstName, user.LastName, user.NickName, user.Gender,user.WorkTime,user.Wage,user.DateStart, user.Position, user.Phone, user.Address);
            DatabaseManager.Write(userDt);

        }

        public static void UpdateUser(User user)
        {
            DataTable userDt = DatabaseManager.Read("User");

            foreach (DataRow row in userDt.Rows)
            {
                if (row["id"].Equals(user.Id))
                {
                    row["username"] = user.Username;
                    row["password"] = user.Password;
                    row["firstname"] = user.FirstName;
                    row["lastname"] = user.LastName;
                    row["nickname"] = user.NickName;
                    row["gender"] = user.Gender;
                    row["worktime"] = user.WorkTime;
                    row["wage"] = user.Wage;
                    row["datestart"] = user.DateStart;
                    row["position"] = user.Position;
                    row["phone"] = user.Phone;
                    row["address"] = user.Address;
                    break;
                }
            }
            DatabaseManager.Write(userDt);
        }

        public static void DeleteUser(string id)
        {
            DataTable userDt = DatabaseManager.Read("User");

            foreach (DataRow row in userDt.Rows)
            {
                if (row["id"].Equals(id))
                {
                    row.Delete();
                    break;
                }
            }
            DatabaseManager.Write(userDt);
        }

        public static User GetUser(string id)
        {
            User user = null;
            DataTable userDt = DatabaseManager.Read("User");

            foreach (DataRow row in userDt.Rows)
            {
                if (((string)row["id"]).Equals(id))
                {
                    user = new User
                               {
                                   Id = (string)row["id"],
                                   Username = (string)row["username"],
                                   Password = (string)row["password"],
                                   FirstName = (string)row["firstname"],
                                   LastName = (string)row["lastname"],
                                   NickName = (string)row["nickname"],
                                   Gender = (string)row["gender"],
                                   WorkTime = (string)row["worktime"],
                                   Wage = decimal.Parse(row["wage"].ToString()),
                                   DateStart = string.IsNullOrEmpty(row["datestart"] as string) ? DateTime.Now : DateTime.Parse(row["datestart"].ToString()),
                                   Position = int.Parse(row["position"].ToString()),
                                   Phone = (string)row["phone"],
                                   Address = (string)row["address"]
                               };
                    break;
                }
            }
            return user;
        }

        public static IList<User> GetAllUsers()
        {
            IList<User> users = null;
            DataTable userDt = DatabaseManager.Read("User");
            if (userDt.Rows.Count > 0)
            {
                users = new List<User>();
                foreach (DataRow row in userDt.Rows)
                {
                    users.Add(new User
                                  {
                                      Id = (string)row["id"],
                                      Username = (string)row["username"],
                                      Password = (string)row["password"],
                                      FirstName = (string)row["firstname"],
                                      LastName = (string)row["lastname"],
                                      NickName = (string)row["nickname"],
                                      Gender = (string)row["gender"],
                                      WorkTime = (string)row["worktime"],
                                      Wage = decimal.Parse(row["wage"].ToString()),
                                      DateStart = string.IsNullOrEmpty(row["datestart"] as string) ? DateTime.Now : DateTime.Parse(row["datestart"].ToString()),
                                      Position = int.Parse(row["position"].ToString()),
                                      Phone = (string)row["phone"],
                                      Address = (string)row["address"]
                                  });
                }
            }
            return users;
        }

        public static bool UserExist(string username)
        {
            bool exist = false;
            DataTable userDt = DatabaseManager.Read("User");

            foreach (DataRow row in userDt.Rows)
            {
                if (row["username"].Equals(username))
                {
                    exist = true;
                    break;
                }
            }
            return exist;
        }
    }
}
